package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.RoleMapper;
import com.example.demo.pojo.Role;
import com.example.demo.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class EndRoleController {


    @Autowired
    private RoleMapper roleMapper;


    /**
     * 获取角色列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/get")
    public Object get(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> roles = roleMapper.selectList(queryWrapper);
        Integer count = roleMapper.selectCount(queryWrapper);
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        return LayuiResult.LayuiData(0,"success",pageInfo.getList(),count);
    }


    /**
     * 查询所有角色给添加用户功能
     * @return
     */
    @RequestMapping("/getRoleForAddUser")
    public Object getRoleForAddUser(){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        List<Role> roles = roleMapper.selectList(queryWrapper);
        return LayuiResult.LayuiData(0,"success",roles);
    }


    /**
     * 修改角色信息
     * @param role
     * @return
     */
    @RequestMapping("/update")
    public Object update(@RequestBody Role role){
        roleMapper.updateById(role);
        return LayuiResult.LayuiData(0,"修改成功",null);
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id){
        Role role = new Role();
        role.setId(id);
        role.setStatus(1);
        roleMapper.updateById(role);
        return LayuiResult.LayuiData(0,"删除成功",null);
    }

    /**
     * 恢复删除
     * @param id
     * @return
     */
    @RequestMapping("/back/{id}")
    public Object back(@PathVariable Integer id){
        Role role = new Role();
        role.setId(id);
        role.setStatus(0);
        roleMapper.updateById(role);
        return LayuiResult.LayuiData(0,"恢复成功",null);
    }


    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Object add(@RequestBody Role role){
        roleMapper.insert(role);
        return LayuiResult.LayuiData(0,"添加角色成功",null);
    }

}
